package com.ticketbooking.controller.OrderManager;

import com.ticketbooking.constant.Constant;
import com.ticketbooking.service.IOrderInfDao;
import com.ticketbooking.util.SecurityTool;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import com.ticketbooking.entity.OrderInfEntity;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/ordermanager")
public class OrderManagerController {
    @Resource
    private IOrderInfDao orderInfDao;

    @CrossOrigin(origins = "http://localhost:8080", maxAge = 3600)
    @RequestMapping("/getOrderInf")
    public String getOrderInfMsg(HttpServletRequest request, Model model){
        List orderInfList = null;
        if(SecurityTool.isReturnJson("2",request)){
            orderInfList = orderInfDao.getAllOrderInf();
        }
        model.addAttribute("orderInfList",orderInfList);
        return Constant.JSON_VIEW_OTHER;
    }

    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping("/getOrderInfById")
    public String getOrderInfMsgId(HttpServletRequest request, Model model,String ID){
        List orderInfList = null;
        if(SecurityTool.isReturnJson("2",request)){
            orderInfList = orderInfDao.getOrderInfByOrderId(ID);
        }
        model.addAttribute("orderIn",orderInfList);
        return Constant.JSON_VIEW_OTHER;
    }

    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping("/getOrderInfByUserId")
    public String getOrderInfMsgUserId(HttpServletRequest request, Model model,String USERID){
        List orderInfList = null;
        if(SecurityTool.isReturnJson("2",request)){
            orderInfList = orderInfDao.getOrderInfByUserId(USERID);
        }
        model.addAttribute("orderInfList",orderInfList);
        return Constant.JSON_VIEW_OTHER;
    }

    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping("/addOrderInf")
    @ResponseBody
    public String addOrderInf(HttpServletRequest request){
//        String orderid= request.getParameter("orderid");
        String routeid= request.getParameter("routeid");
        String userid= request.getParameter("userid");
        String seatnum= request.getParameter("seatnum");
        String fare= request.getParameter("fare");
        String booktime= request.getParameter("booktime");
        String state= request.getParameter("state");
        String deptime= request.getParameter("deptime");
        OrderInfEntity routeinf = new OrderInfEntity();
        routeinf.setBooktime(booktime);
        routeinf.setDeptime(deptime);
        routeinf.setState(state);
        routeinf.setSeatnum(seatnum);
        routeinf.setFare(fare);
//        routeinf.setOrderid(orderid);
        routeinf.setUserid(userid);
        routeinf.setRouteid(routeid);
        orderInfDao.save(routeinf);
        return "true";
    }

    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping("/getOrderInfnum")
    public String getOrderInfnum(HttpServletRequest request, Model model){
        List orderInfList = null;
        if(SecurityTool.isReturnJson("2",request)){
            orderInfList = orderInfDao.getOrderInfnum();
        }
        model.addAttribute("orderInfList",orderInfList);
        return Constant.JSON_VIEW_OTHER;
    }

}
